package com.topera.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.topera.commonutils.R;
import com.topera.eduservice.entity.EduTeacher;
import com.topera.eduservice.entity.vo.TeacherQuery;
import com.topera.eduservice.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author puzhenjiang
 * @since 2020-04-27
 */
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;//

    //访问地址：http://localhost:8001/eduservice/teacher/findAll

    //1、查询讲师表中所有数据
    //rest风格
    /**
     * 查询所有讲师数据
     * @return
     */
    @GetMapping("findAll")
    public R findAll(){
        List<EduTeacher> teacherList = eduTeacherService.list(null);
        return R.ok().data("items",teacherList);
        //return teacherList;
    }

    //swagger的访问地址：http://localhost:8001/swagger-ui.html
    @DeleteMapping("{id}")
    public R removeTeacher(@PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);
        if(flag){
           return  R.ok();
        }else{
           return  R.error();
        }
    }

    /**
     * 分页查询
     * @param current  当前页
     * @param limit  每页显示多少
     * @return
     */
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageTeacher(@PathVariable long current,@PathVariable long limit){

        //创建Page对象
        Page<EduTeacher> pageTeacher = new Page<EduTeacher>(current,limit);
        //底层进行封装 把分页所有数据封装到pageTeacher对象中
        eduTeacherService.page(pageTeacher,null);
        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords();//记录
        Map result = new HashMap();
        result.put("total",total);
        result.put("records",records);
        return R.ok().data(result);
    }

    /**
     * 知识点：
     *          RequestBody:使用json传递数据，把json数据封装 到对象里面，并且提交方式要是post  require=false  表示参数值非必须
     *          ResponseBody:返回以json格式的数据
     * 多条件组合查询
     * @param current
     * @param limit
     * @param teacherQuery
     * @return
     */
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false)  TeacherQuery teacherQuery){

        //创建Page对象
        Page<EduTeacher> pageTeacher = new Page<EduTeacher>(current,limit);
        //构造条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件查询
        if(teacherQuery!=null){
            String name = teacherQuery.getName();//姓名
            Integer level = teacherQuery.getLevel();//级别
            String start = teacherQuery.getBegin();//开始时间
            String end = teacherQuery.getEnd();//结束时间
            if(!StringUtils.isEmpty(name)){
                wrapper.like("name",name);
            }
            if(!StringUtils.isEmpty(level)){
                wrapper.eq("level",level);
            }
            if(!StringUtils.isEmpty(start)){
                wrapper.ge("gmt_create",start);//注意第一个参数是数据库中表中的字段名  不是实体中的值
            }
            if(!StringUtils.isEmpty(end)){
                wrapper.le("gmt_create",end);
            }

        }
        wrapper.orderByDesc("gmt_create");
        //底层进行封装 把分页所有数据封装到pageTeacher对象中
        eduTeacherService.page(pageTeacher,wrapper);

        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords();//记录
        Map result = new HashMap();
        result.put("total",total);
        result.put("records",records);
        return R.ok().data(result);
    }

    /**
     * 添加讲师
     * @param teacher
     * @return
     */
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher teacher){
      boolean result =   eduTeacherService.save(teacher);
      if(result){
          return R.ok();
      }else{
          return R.error();
      }
    }

    /**
     * 根据id获取讲师id
     * @param id
     * @return
     */
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }

    /**
     * 修改讲师信息
     * @param teacher
     * @return
     */
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher teacher ){
        boolean result = eduTeacherService.updateById(teacher);
        if(result){
          return R.ok();
        }else{
            return R.error();
        }
    }

}

